import { createMachine } from "xstate";

export const recorderMachine = createMachine({
  context: {},
  id: "Recorder",
  initial: "recorder_stopped",
  states: {
    recorder_stopped: {
      on: {
        start_record: {
          target: "recorder_starting",
        },
      },
      entry: {
        type: "recorder_stopped_entry",
      },
    },
    recorder_starting: {
      invoke: {
        id: "start_record",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "recorder_started",
          actions: {
            type: "start_record_done",
          },
        },
        onError: {
          target: "recorder_stopped",
          actions: {
            type: "start_record_error",
          },
        },
        src: "start_record",
      },
    },
    recorder_started: {
      on: {
        stop_record: {
          target: "recorder_stopping",
        },
      },
      entry: {
        type: "recorder_started_entry",
      },
    },
    recorder_stopping: {
      invoke: {
        id: "stop_record",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "recorder_stopped",
          actions: {
            type: "stop_record_done",
          },
        },
        onError: {
          target: "recorder_stopped",
          actions: {
            type: "stop_record_error",
          },
        },
        src: "stop_record",
      },
    },
  },
});